Test Series - java script

Test Number 16/92

Q: The ‘$’ present in the RegExp object is called a ____________
A. character
B. matcher
C. metacharacter
D. metadata
Solution: The ‘S’ is a special metacharacter that matches the end of a string. It is used to define or access elements in jquery.
Q: Consider the following JavaScript statement containing regular expressions and check if the pattern matches?

var text = "testing: 1, 2, 3"; 
var pattern = /d+/g;
A. text==pattern
B. text.equals(pattern)
C. text.test(pattern)
D. pattern.test(text)
Solution: The given pattern is applied to the text given in the parenthesis. The test() method tests for a match in a string. This method returns true if it finds a match, otherwise it returns false.
Q: The regular expression to match any one character not between the brackets is __________
A. […]
B. [^]
C. [^…]
D. [D]
Solution: RegExp defines a special set of character that is used to do manipulation on strings and other variables. The [^…] character class is used to match or draw any one character not between the brackets.
Q: What does /[^(]* regular expression indicate?
A. Match one or more characters that are not open parenthesis
B. Match zero or more characters that are open parenthesis
C. Match zero or more characters that are not open parenthesis
D. Match one or more characters that are open parenthesis
Solution: The [^…] character class is used to match or draw any one character not between the brackets. One should always be careful while using * and ? as repetition characters as they may match zero instances of whatever precedes them, they are allowed to match nothing.
Q: What will be the result when non greedy repetition is used on the pattern /a+?b/?
A. Matches the letter b preceded by the fewest number of a’s possible
B. Matches the letter b preceded by any number of a
C. Matches letter a preceded by letter b, in the stack order
D. Matches letter a present in the string
Solution: Using non greedy repetition may not always produce the results you expect. /a+?b/ matches the letter b preceded by the fewest number of a’s possible.
Q: What does the subexpression /java(script)?/ result in?
A. It matches “java” followed by the optional “script”
B. It matches “java” followed by any number of “script”
C. It matches “java” followed by a minimum of one “script”
D. It matches “java” followed by a single “script”
Solution: paranthesis () check for the optional presence of the argument in the string. subexpression /java(script)?/ matches “java” followed by the optional “script”.
Q: What is the most essential purpose of parentheses in regular expressions?
A. Define pattern matching techniques
B. Define subpatterns within the complete pattern
C. Define portion of strings in the regular expression
D. matching the complete string
Solution: When a regular expression is successfully matched against a target string, it is possible to extract the portions of the target string that matched any particular paranthesized subpattern. The essential purpose of parantheses in regular expressions is to define subpatterns within the complete pattern.
Q: The method that performs the search-and-replace operation to strings for pattern matching is _______
A. searchandreplace()
B. add()
C. edit()
D. replace()
Solution: The replace() method performs a search-and-replace operation. It takes a regular expression as its first argument and a replacement string as its second argument.
Q: What would be the result of the following statement in JavaScript using regular expression methods?
A. Returns [“123″”456″”789”]
B. Returns [“123″,”456″,”789”]
C. Returns [1,2,3,4,5,6,7,8,9]
D. Throws an exception
Solution: The split() method can take regular expressions as its arguments. The split() method generally breaks the string on which it is called into an array of substrings, using the argument as a separator.
Q: What will be the output of the following JavaScript code?

    System.out.println(Pattern.matches("[^abc]", "aemngq"));
A. true
B. false
C. undefined
D. 1
Solution: “^” is used as a negation operator. The above code will print false as “a” is present in the string passed in the argument.

You Have Score    /10